package com.ima;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dev.GUI;
import com.dev.IconView;
import com.dev.IconViewClose;
import com.dev.MainActivity;

class QueryForSelectionChannel extends LinearLayout implements OnClickListener {
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	LinearLayout titlebar;
	TextView title;
	IconViewClose close;
	TextView itReverse;
	TextView itEarse;
	TextView itFill;
	TextView itShut;
	ImageManipulationPage imp;
	MainActivity ma;
	QueryForSelectionChannel(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackground(new GUI.BorderDrawable());
		int padding=9;
		int textSize=(int)(13*ma.dp);
		close=new IconViewClose(ma);
		close.setOnClickListener(this);
		title=new TextView(ma);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setPadding(padding,0,0,0);
		title.getPaint().setTextSize(textSize);
		titlebar=new LinearLayout(ma);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.setBackground(new GUI.BorderDrawable());
		titlebar.addView(title,new LayoutParams(0,-1,1f));
		titlebar.addView(close);
		itReverse=new TextView(ma);
		itReverse.getPaint().setTextSize(textSize);
		itReverse.setGravity(16);
		itReverse.setPadding(padding,0,0,0);
		itReverse.setText("反选当前选区");
		itReverse.setTextColor(0xff000000);
		itReverse.setOnClickListener(this);
		itFill=new TextView(ma);
		itFill.getPaint().setTextSize(textSize);
		itFill.setGravity(16);
		itFill.setPadding(padding,0,0,0);
		itFill.setText("涂色选区");
		itFill.setTextColor(0xff000000);
		itFill.setOnClickListener(this);
		itEarse=new TextView(ma);
		itEarse.getPaint().setTextSize(textSize);
		itEarse.setGravity(16);
		itEarse.setPadding(padding,0,0,0);
		itEarse.setText("擦除选区");
		itEarse.setTextColor(0xff000000);
		itEarse.setOnClickListener(this);
		itShut=new TextView(ma);
		itShut.getPaint().setTextSize(textSize);
		itShut.setGravity(16);
		itShut.setPadding(padding,0,0,0);
		itShut.setText("关闭选区");
		itShut.setTextColor(0xff000000);
		itShut.setOnClickListener(this);
		LayoutParams itlp=new LayoutParams(-1,imp.dialogHorItemHeight);
		addView(titlebar,itlp);
		addView(itReverse,itlp);
		addView(itFill,itlp);
		addView(itEarse,itlp);
		addView(itShut,itlp);
	}
	final static String titleText="选区通道";
	void show(ImageManipulationPage imp){
		if(imp.cv.selected==null){
			title.setText(titleText+"（不可用）");
		}else{
			title.setText(titleText);
		}
		this.imp=imp;
		wlp.width=ma.wp*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	final String noSelection="当前图层尚未创建选区通道";
	@Override
	public void onClick(View view) {
		if(view==close){//onClick
			dismiss();
		}else if(view==itReverse){//onClick
			if(imp.cv.selected==null){
				Toast.makeText(ma,noSelection,0).show();
			}else{
				byte[] selected=imp.cv.selected;
				SelectionFrame sf=new SelectionFrame(selected);
				imp.undoable.push(sf);
				ma.gui.ivUndo.setColorFilter(null);
				ma.gui.ivUndo.invalidate();
				imp.redoable.clear();
				ma.gui.ivRedo.setColorFilter(IconView.unable);
				ma.gui.ivRedo.invalidate();
				int w=imp.cv.iw;
				int h=imp.cv.ih;
				for(int x=0;x<w;x++){
					for(int y=0;y<h;y++){
						int v=(0|selected[x+w*y])>>>24;
						final int i=255;
						selected[x+w*y]=(byte)(i-v);
					}
				}
				imp.makeMask(selected);
				imp.cv.invalidate();
			}
		}else if(view==itEarse){//onClick
			byte[] selected=imp.cv.selected;
			Bitmap image=imp.cv.image;
			if(selected==null){
				Toast.makeText(ma,noSelection,0).show();
			}else{
				SelectionFrame sf=new SelectionFrame(selected);
				imp.undoable.push(sf);
				imp.cv.atImageChange();
				int iw=image.getWidth();
				int ih=image.getHeight();
				int bgcolor=imp.backgroundColor;
				int aa=bgcolor>>>24&0xff;
				int rr=bgcolor>>>16&0xff;
				int gg=bgcolor>>>8&0xff;
				int bb=bgcolor&0xff;
				for(int x=0;x<iw;x++){
					for(int y=0;y<ih;y++){
						int px=image.getPixel(x,y);
						int sel=(0|selected[x+iw*y]);
						int ina=px>>>24&0xff;
						int inr=px>>>16&0xff;
						int ing=px>>>8&0xff;
						int inb=px&0xff;
						int out=(ina+(aa-ina)*sel/255)<<24;
						out+=0x00ff0000&((inr+(rr-inr)*sel/255)<<16);
						out+=0x0000ff00&((ing+(gg-ing)*sel/255)<<8);
						out+=0x000000ff&(inb+(bb-inb)*sel/255);
						image.setPixel(x,y,out);
					}
				}
				Canvas cvs=new Canvas(imp.cv.imageMask);
				cvs.drawColor(0x00ffffff,PorterDuff.Mode.SRC);
				imp.cv.invalidate();
			}
		}else if(view==itFill){//onClick
			byte[] selected=imp.cv.selected;
			Bitmap image=imp.cv.image;
			Bitmap imageMask=imp.cv.imageMask;
			if(selected!=null){
				imp.cv.atImageChange();
				Canvas canvas=new Canvas(image);
				Paint p=new Paint();
				int id=canvas.saveLayer(null,null);
				canvas.drawBitmap(imageMask,0,0,p);
				canvas.drawColor(imp.foregroundColor,PorterDuff.Mode.SRC_IN);
				canvas.restoreToCount(id);
				Canvas mask=new Canvas(imageMask);
				mask.drawColor(0x00ffffff,PorterDuff.Mode.CLEAR);
				imp.cv.selected=null;
				imp.cv.invalidate();
				ma.wm.removeView(this);
			}else{
				Toast.makeText(ma,noSelection,0).show();
			}
		}else if(view==itShut){
			if(imp.cv.selected==null){
				Toast.makeText(ma,noSelection,0).show();
			}else{
				SelectionFrame sf=new SelectionFrame(imp.cv.selected);
				imp.undoable.push(sf);
				ma.gui.ivUndo.setColorFilter(null);
				ma.gui.ivUndo.invalidate();
				imp.redoable.clear();
				ma.gui.ivRedo.setColorFilter(IconView.unable);
				ma.gui.ivRedo.invalidate();
				Canvas cvs=new Canvas(imp.cv.imageMask);
				cvs.drawColor(0x00ffffff,PorterDuff.Mode.SRC);
				imp.cv.selected=null;
				imp.cv.invalidate();
			}
		}
	}
}
